package com.osmino.contacts.sideindex;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 1.SetListener 2.call refreshIndex(String[] items)
 */

public class AlphabetIndexView extends LinearLayout {

	private static final boolean DBG = true;
	private Context mContext;
	private GestureDetector mGestureDetector;
	private static float mSideIndexX;
	private static float mSideIndexY;
	private int mSideIndexViewHeight;
	private int mIndexArrayListSize;
	private ArrayList<Object[]> mIndexArrayList = null;
	private String[] mItems;

	private AlphabetIndexListener mListener = new AlphabetIndexListener() {
		@Override
		public void onComputeListItem(int item) {
			if (DBG)
				System.out.println("onComputeListItem()=" + item);
		}

		@Override
		public void onScroll() {
			if (DBG)
				System.out.println("onScroll()");
		}

	};

	public void setAlphabetIndexListener(AlphabetIndexListener listener) {
		mListener = listener;
	}

	public AlphabetIndexView(Context activity) {
		super(activity);
		mContext = activity;
		mGestureDetector = new GestureDetector(mContext, new SideIndexGestureListener());
	}

	public AlphabetIndexView(Context activity, AttributeSet attrs) {
		super(activity, attrs);
		mContext = activity;
		mGestureDetector = new GestureDetector(mContext, new SideIndexGestureListener());
	}

	public boolean onTouchEvent(MotionEvent event) {
		mGestureDetector.onTouchEvent(event);
		return true;
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		refreshIndex(mItems);
	}

	public void refreshIndex(String[] items) {
		if (items == null || items.length == 0 || getHeight() == 0) {
			if (getHeight() == 0) {
				mItems = items;
			}
			return;
		} else {
			setVisibility(View.VISIBLE);
		}
		mSideIndexViewHeight = getHeight();
		removeAllViews();
		TextView tmpTV = null;
		mIndexArrayList = createIndex(items);
		mIndexArrayListSize = mIndexArrayList.size();
		int indexMaxSize = (int) Math.floor(getHeight() / 20);
		int tmpIndexListSize = mIndexArrayListSize;
		while (tmpIndexListSize > indexMaxSize) {
			tmpIndexListSize = tmpIndexListSize / 2;
		}
		double delta = mIndexArrayListSize / tmpIndexListSize;
		String tmpLetter = null;
		Object[] tmpIndexItem = null;
		for (double i = 1; i <= mIndexArrayListSize; i = i + delta) {
			tmpIndexItem = mIndexArrayList.get((int) i - 1);
			
			//add dots
			
			if(!TextUtils.isEmpty(tmpLetter) && !TextUtils.isEmpty(tmpIndexItem[0].toString())){
				char oldChar = tmpLetter.charAt(0);
				char newChar = tmpIndexItem[0].toString().charAt(0);
				if((newChar - oldChar)>1 && tmpIndexListSize<10){
					tmpTV = new TextView(getContext());
					tmpTV.setText("\u00B7");
					tmpTV.setGravity(Gravity.CENTER);
					tmpTV.setTextSize(24);
					tmpTV.setPadding(0, 0, 0, 0);
					tmpTV.setMaxHeight(12);
					tmpTV.setTextColor(Color.parseColor("#4e4e4e"));
					LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);
					tmpTV.setLayoutParams(params);
					addView(tmpTV);
				}
			}
			
			//
			
			tmpLetter = tmpIndexItem[0].toString();
			tmpTV = new TextView(getContext());
			tmpTV.setText(tmpLetter.toUpperCase());
			tmpTV.setGravity(Gravity.CENTER);
			tmpTV.setTextSize(12);
			tmpTV.setTextColor(Color.parseColor("#4e4e4e"));
			LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);
			tmpTV.setLayoutParams(params);
			addView(tmpTV);
		}
		setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				mSideIndexX = event.getX();
				mSideIndexY = event.getY();
				displayListItem();
				return false;
			}
		});
		mItems = null;
	}

	private ArrayList<Object[]> createIndex(String[] strArr) {
		ArrayList<Object[]> tmpIndexList = new ArrayList<Object[]>();
		Object[] tmpIndexItem = null;
		int tmpPos = 0;
		String tmpLetter = "";
		String currentLetter = null;
		String strItem = null;
		for (int j = 0; j < strArr.length; j++) {
			strItem = strArr[j];
			currentLetter = strItem.substring(0, 1);
			if (!currentLetter.equals(tmpLetter)) {
				tmpIndexItem = new Object[3];
				tmpIndexItem[0] = tmpLetter;
				tmpIndexItem[1] = tmpPos - 1;
				tmpIndexItem[2] = j - 1;
				tmpLetter = currentLetter;
				tmpPos = j + 1;
				tmpIndexList.add(tmpIndexItem);
			}
		}
		tmpIndexItem = new Object[3];
		tmpIndexItem[0] = tmpLetter;
		tmpIndexItem[1] = tmpPos - 1;
		tmpIndexItem[2] = strArr.length - 1;
		tmpIndexList.add(tmpIndexItem);
		if (tmpIndexList != null && tmpIndexList.size() > 0) {
			tmpIndexList.remove(0);
		}
		return tmpIndexList;
	}

	private class SideIndexGestureListener extends GestureDetector.SimpleOnGestureListener {
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
			mSideIndexX = mSideIndexX - distanceX;
			mSideIndexY = mSideIndexY - distanceY;
			if (mSideIndexX >= 0 && mSideIndexY >= 0) {
				displayListItem();
				mListener.onScroll();
			}
			return true;
		}
	}

	private void displayListItem() {
		double pixelPerIndexItem = (double) mSideIndexViewHeight / mIndexArrayListSize;
		int itemPosition = (int) (mSideIndexY / pixelPerIndexItem);
		int minPosition = (int) (itemPosition * pixelPerIndexItem);
		if (itemPosition < (mIndexArrayList.size()) && itemPosition > -1) {
			Object[] indexItem = mIndexArrayList.get(itemPosition);
			int indexMin = Integer.parseInt(indexItem[1].toString());
			int indexMax = Integer.parseInt(indexItem[2].toString());
			int indexDelta = Math.max(1, indexMax - indexMin);
			double pixelPerSubitem = pixelPerIndexItem / indexDelta;
			int subitemPosition = (int) (indexMin + (mSideIndexY - minPosition) / pixelPerSubitem);
			mListener.onComputeListItem(subitemPosition);
		}
	}

	public interface AlphabetIndexListener {
		public void onComputeListItem(int item);

		public void onScroll();
	}
}
